package com.guigu.pd.work.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.serviceapi.omsfeign.OrderCargoFeign;
import com.guigu.pd.serviceapi.pojo.OrderCargoEntity;
import com.guigu.pd.oms.service.OrderCargoService;
import com.guigu.pd.serviceapi.authfeign.SsqFeign;
import com.guigu.pd.serviceapi.omsfeign.OrderFeign;
import com.guigu.pd.serviceapi.pojo.OrderEntity;
import com.guigu.pd.serviceapi.pojo.TaskPickupDispatchEntity;
import com.guigu.pd.serviceapi.workfeign.TaskPickupDispatchFeign;
import com.guigu.pd.work.entity.TransportOrderEntity;
import com.guigu.pd.work.service.TaskPickupDispatchService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.work.dao.TransportOrderDao;
import com.guigu.pd.work.service.TransportOrderService;


@Service("transportOrderService")
public class TransportOrderServiceImpl extends ServiceImpl<TransportOrderDao, TransportOrderEntity> implements TransportOrderService {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private SsqFeign ssqFeign;

    @Autowired(required = false)
    OrderCargoService orderCargoService;

    @Autowired
    TaskPickupDispatchFeign taskPickupDispatchFeign;

    @Autowired(required = false)
    OrderCargoFeign orderCargoFeign;

    @Autowired
    UserFeign userFeign;

    @Autowired
    CoreOrgFeign coreOrgFeign;


    @Override
    public PageVo<TransportOrderEntity> queryPage(Map<String, Object> params) throws ParseException {
        TransportOrderEntity
                transportOrder = JSON.parseObject(JSON.toJSONString(params),
                TransportOrderEntity.class);
        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        LambdaQueryWrapper<TransportOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(transportOrder.getId()),
                TransportOrderEntity::getId, transportOrder.getId());

        queryWrapper.like(transportOrder.getStatus() != null,
                TransportOrderEntity::getStatus, transportOrder.getStatus());

        if (StringUtils.isNotEmpty(transportOrder.getSenderaddress())&&!transportOrder.getSenderaddress().equals(",,")){
            String senderaddress = transportOrder.getSenderaddress();
            String[] split = senderaddress.split(",");
            Integer shenid = Integer.valueOf(split[0]);
            Integer shiid = Integer.valueOf(split[1]);
            Integer quid = Integer.valueOf(split[2]);
            String shen = ssqFeign.getnamebyId(shenid);
            String shi = ssqFeign.getnamebyId(shiid);
            String qu = ssqFeign.getnamebyId(quid);
            String address = shen+shi+qu;
            String orderid = orderFeign.getbySendaddress(address);
            queryWrapper.like(StringUtils.isNotBlank(transportOrder.getSenderaddress()),
                    TransportOrderEntity::getOrderId, orderid);
        }
        if (StringUtils.isNotBlank(transportOrder.getSendername())){
            String orderid = orderFeign.getbySendname(transportOrder.getSendername());
            queryWrapper.like(StringUtils.isNotBlank(transportOrder.getSendername()),
                    TransportOrderEntity::getOrderId, orderid);
        }
        if (StringUtils.isNotBlank(transportOrder.getSenderphone())){
            String orderid = orderFeign.getbySendphone(transportOrder.getSenderphone());
            queryWrapper.like(StringUtils.isNotBlank(transportOrder.getSenderphone()),
                    TransportOrderEntity::getOrderId, orderid);
        }
        if (StringUtils.isNotBlank(transportOrder.getReceiveraddress())&&!transportOrder.getReceiveraddress().equals(",,")){
            String receiveraddress = transportOrder.getReceiveraddress();
            String[] split = receiveraddress.split(",");
            String shen = ssqFeign.getnamebyId(Integer.parseInt(split[0]));
            String shi = ssqFeign.getnamebyId(Integer.parseInt(split[1]));
            String qu = ssqFeign.getnamebyId(Integer.parseInt(split[2]));
            String address = shen+shi+qu;
            String orderid = orderFeign.getbyReceiveraddress(address);
            queryWrapper.like(StringUtils.isNotBlank(transportOrder.getReceiveraddress()),
                    TransportOrderEntity::getOrderId, orderid);
        }
        if (StringUtils.isNotBlank(transportOrder.getReceivername())){
            String orderid = orderFeign.getbyReceivername(transportOrder.getReceivername());
            queryWrapper.like(StringUtils.isNotBlank(transportOrder.getReceivername()),
                    TransportOrderEntity::getOrderId, orderid);
        }
        if (StringUtils.isNotBlank(transportOrder.getReceiverphone())){
            String orderid = orderFeign.getbyReceiverphone(transportOrder.getReceiverphone());
            queryWrapper.like(StringUtils.isNotBlank(transportOrder.getReceiverphone()),
                    TransportOrderEntity::getOrderId, orderid);
        }

        PageHelper.startPage(pageno, pagesize);
        List<TransportOrderEntity> transportOrderlist = this.list(queryWrapper);

        for (TransportOrderEntity transport : transportOrderlist) {
            String orderId = transport.getOrderId();
            //订单查询
            String order = orderFeign.getroderbyId(orderId);
            Date date = orderFeign.gettimebyId(orderId);
            String[] split = order.split(",");
            transport.setOrdercreateTime(date);
            transport.setSendername(split[1]);
            transport.setSenderphone(split[2]);
            transport.setSenderaddress(split[3]);
            transport.setReceivername(split[4]);
            transport.setReceiverphone(split[5]);
            transport.setReceiveraddress(split[6]);

        }
        PageInfo<TransportOrderEntity> pageInfo = new PageInfo<TransportOrderEntity>(transportOrderlist);

        PageVo<TransportOrderEntity> pageVo = new PageVo<TransportOrderEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public TransportOrderEntity getinfobyId(String id) {
        TransportOrderEntity transportOrder = this.getById(id);
        String orderid = transportOrder.getOrderId();
        OrderEntity order = orderFeign.getroderinfobyId(orderid);
        transportOrder.setOrderinfo(order);

        List<OrderCargoEntity> ordercargolist = new ArrayList<OrderCargoEntity>();
        OrderCargoEntity orderCargo = orderCargoFeign.getordercargobyId(orderid);
        ordercargolist.add(orderCargo);
        transportOrder.setOrdercargo(ordercargolist);

        TaskPickupDispatchEntity pickupDispatch = taskPickupDispatchFeign.getbyorderId(orderid);
        String agencyname = coreOrgFeign.getcornamebyid(pickupDispatch.getAgencyId());
        String userstr = userFeign.getuserbyId(pickupDispatch.getCourierId());
        String[] split = userstr.split(",");
        if(split.length==2){
            pickupDispatch.setCourier(split[0]);
            pickupDispatch.setCphone(split[1]);
        }
        if (split.length==1){
            pickupDispatch.setCourier(split[0]);
        }
        pickupDispatch.setAgencyname(agencyname);
        transportOrder.setTaskpt(pickupDispatch);
        return transportOrder;
    }

}